﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core;

namespace Web.Controllers
{
    public class systemController : JsonController
    {
        //
        // GET: /system/

        public ActionResult getSystemSettings()
        {
            return Json(system.getSystemSettings());
        }

        [OnlyAdmin]
        public ActionResult becomeGod()
        {
            Certify.user.god = true;
            return Json(new
            {
                success = true
            });
        }

        [OnlyGod]
        public ActionResult becomeUnGod()
        {
            Certify.user.god = false;
            return Json(new
            {
                success = true
            });
        }

        [OnlyGod]
        public ActionResult editSystemSettings(string title, string copyright,
            string openAccess, string openOutsideReg, string openOutsideSendMsg, string openDiscuss, string openOutsideDiscuss)
        {
            CheckEmpty(title, copyright);
            return Json(system.editSystemSettings(title, copyright,
                openAccess.ToBool(), openOutsideReg.ToBool(), openOutsideSendMsg.ToBool(), openDiscuss.ToBool(), openOutsideDiscuss.ToBool()));
        }

        public ActionResult getLangList(string all)
        {
            return Json(system.getLangList(all.ToBool()));
        }

        [OnlyGod]
        public ActionResult getLangInfo(int code)
        {
            return Json(system.getLangInfo(code));
        }

        [ValidateInput(false)]
        [OnlyGod]
        public ActionResult addOrEditLangInfo(int code, int? oldCode, string title, string disabled,
            string standardTest, string standardCode, int? standardTime, int? standardMem, string safeTest, string safeRegExp)
        {
            CheckEmpty(title);
            return Json(system.addOrEditLangInfo(code, oldCode, title, disabled.ToBool(),
                standardTest.ToBool(), standardCode, standardTime, standardMem, safeTest.ToBool(), safeRegExp));
        }

        [OnlyGod]
        public ActionResult deleteLang(int code)
        {
            return Json(system.deleteLang(code));
        }

        public ActionResult json()
        {
            IsFileUpload = (bool)(TempData["IsFileUpload"] ?? false);
            return Json(TempData["json"]);
        }

        [OnlyGod]
        public ActionResult uploadFile(HttpPostedFileBase userfile)
        {
            IsFileUpload = true;
            CheckNull(userfile);
            var filename = userfile.FileName;
            var ext = System.IO.Path.GetExtension(filename).ToLower();
            bool ok = false;
            foreach (var e in new[] { ".jpg", ".bmp", ".gif", ".png", ".doc", ".xls", ".pdf", ".docx", ".xlsx", ".ppt", ".pptx" })
                if (ext == e)
                {
                    ok = true;
                    break;
                }
            if (!ok) return Json(new
            {
                success = false,
                msg = "不支持上传的格式！"
            });
            return Json(system.uploadFile(userfile, ext));
        }
    }
}
